package com.example.hu;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.example.hu.Appliction.MyApp;
import com.example.hu.UI.BaseActivity;
import com.example.hu.UI.FaceSearchActivity;
import com.example.hu.UI.FaceSettingActivity;
import com.example.hu.UI.LoginActivity;
import com.example.hu.UI.UserEditActivity;

import androidx.appcompat.app.AlertDialog;


public class MainActivity extends BaseActivity {

    private String[] ItemName = { "编辑人员", "人脸设置", "退出登录"
           };
    private ImageButton bst_startmenu;
    private   String   bst;
    private Button  btn_carcode,btn_carcode1,btn_carcode2,btn_carcode3,btn_out;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        if (checkStartType(getIntent())) {
            return;
        }
    }

    private void initView() {

        ItemName[0] = getResources().getString(R.string.menuRead);
        ItemName[1] = getResources().getString(R.string.menuSet);
        ItemName[2] = getResources().getString(R.string.menuLock);
        // hander
        bst_startmenu = (ImageButton) findViewById(R.id.btn_main_systemExit);
        bst_startmenu.setOnClickListener(this);
        btn_carcode=findViewById(R.id.btn_carcode);
        btn_carcode.setText(getString(R.string.btn_carcode));
        btn_carcode.setOnClickListener(this);
        btn_carcode1=findViewById(R.id.btn_carcode1);
        btn_carcode1.setText(getString(R.string.btn_carcode1));
        btn_carcode1.setOnClickListener(this);
        btn_carcode2=findViewById(R.id.btn_carcode2);
        btn_carcode2.setText(getString(R.string.btn_carcode2));
        btn_carcode2.setOnClickListener(this);
        btn_carcode3=findViewById(R.id.btn_carcode3);
        btn_carcode3.setText(getString(R.string.btn_carcode3));
        btn_carcode3.setOnClickListener(this);
        btn_out=findViewById(R.id.btn_out);
        btn_out.setText(getString(R.string.btn_carcode4));
        btn_out.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_main_systemExit:
                StartMenus();

                break;
            case R.id.btn_carcode:
                bst=btn_carcode.getText().toString();
                IntentActivity();

                break;
            case R.id.btn_carcode1:
                bst=btn_carcode1.getText().toString();
                IntentActivity();
                break;
            case R.id.btn_carcode2:
                bst=btn_carcode2.getText().toString();
                IntentActivity();
                break;
            case R.id.btn_carcode3:
                bst=btn_carcode3.getText().toString();
                IntentActivity();
                break;
            case R.id.btn_out:
                bst=btn_out.getText().toString();
                IntentActivity();
                break;
        }
    }

    private void IntentActivity() {
        Intent intent = new Intent(MainActivity.this, FaceSearchActivity.class);
        intent.putExtra("Ufhdata", bst);
        startActivity(intent);
    }


    public void StartMenus() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setItems(ItemName, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                System.out.println(arg1);

                if (arg1 == 0) {
                    Intent intentTo = new Intent();
                    intentTo.setClass(MainActivity.this, UserEditActivity.class);
                    startActivity(intentTo);

                }
                if (arg1 == 1) {

                    Intent intentTo = new Intent();
                    intentTo.setClass(MainActivity.this,
                            FaceSettingActivity.class);
                    startActivity(intentTo);
                }
                if (arg1 == 2) {
                    MainActivity.startActivity(MainActivity.this, MainActivity.StartType.LOGOUT);
                    MainActivity.this.finish();

                }


                arg0.dismiss();
            }
        });
        builder.show();
    }

    @Override
    protected void onNewIntent(Intent paramIntent)
    {
        super.onNewIntent(paramIntent);
        checkStartType(paramIntent);
    }

    public static void startActivity(Context paramContext, StartType paramStartType)
    {
        Intent localIntent = new Intent(paramContext, MainActivity.class);
        localIntent.putExtra("type", paramStartType.getType());
        paramContext.startActivity(localIntent);
    }

    private boolean checkStartType(Intent paramIntent)
    {
        boolean bool = false;
        if (paramIntent.getIntExtra("type", 0) == StartType.LOGOUT.getType())
        {
            MyApp.getApp().setToken(null);
            LoginActivity.startActivity(this);
            finish();
            bool = true;
        }
        return bool;
    }

    public static enum StartType
    {
        LOGOUT(10);

        private int mType;

        private StartType(int paramInt)
        {
            this.mType = paramInt;
        }

        public int getType()
        {
            return this.mType;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
